﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject.Entity;
using SmartProject.Data;

using NUnit.Framework;

namespace SmartProject.Testing
{
    [TestFixture]
    public class Test02_ProductCategory
    {
        private string _categoryName;
        private string _product1;
        private string _product2;
        [Test]
        public void Test01_AddCategoryAndProducts()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    string guid = Guid.NewGuid().ToString();
                    _categoryName = "CategoryA_" + guid;
                    ProductCategory c = new ProductCategory();
                    c.Description = _categoryName;

                    Product p1 = new Product();
                    _product1 = "Product 1 of CategoryA" + guid;
                    p1.Description = _product1;
                    c.Products.Add(p1);

                    Product p2 = new Product();
                    _product2 = "Product 2 of CategoryA" + guid;
                    p2.Description = _product2;
                    c.Products.Add(p2);

                    c.Save(session);
                    transaction.Commit();
                    transaction.Dispose();
                }
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test02_MatchCategoryAndProducts()
        {
            using (var session = DataProvider.Instance.Session)
            {
                ProductCategory c = ProductCategory.GetProductCategory(session, _categoryName);
                Assert.IsNotNull(c);
                Assert.AreEqual(c.Description, _categoryName);
                Assert.AreEqual(c.Products.Count, 2);
                Assert.AreEqual(c.Products[0].Description, _product1);
                Assert.AreEqual(c.Products[1].Description, _product2);
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test03_RemoveCategoryAndProducts()
        {
            using (var session = DataProvider.Instance.Session)
            {
                ProductCategory c = ProductCategory.GetProductCategory(session, _categoryName);
                using (var transaction = session.BeginTransaction())
                {
                    c.Remove(session);
                    transaction.Commit();
                    transaction.Dispose();
                }
                c = ProductCategory.GetProductCategory(session, _categoryName);
                Assert.IsNull(c);
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test04_DeleteCategoryAndProducts()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    ProductCategory.Clean(session);
                    transaction.Commit();
                    transaction.Dispose();
                }

                Assert.AreEqual(0, ProductCategory.GetProductCategories(session).Count);
                session.Close();
                session.Dispose();
            }
        }
    }
}
